package com.hisan.freeride.home.activity;

import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.state.EmptyState;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.databinding.LoveCartDetailsBinding;
import com.hisan.freeride.home.model.Driverinfo;
import com.lzy.okgo.OkGo;
import com.wonderkiln.blurkit.BlurKit;


/**
 * 创建时间 : 2017/6/12
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：爱心车主个人首页
 */
public class LoveCartDetailsActivity extends BaseActivity<LoveCartDetailsBinding> {
    private Driverinfo driverinfos;
    private   int id;

    @Override
    protected int getLayoutId() {
        return  R.layout.love_cart_details;
    }

    @Override
    protected void initView() {
        mBinding.setStateModel(mStateModel);
        noData();
        Bundle bundle=getIntent().getExtras();
        if (!CollectionUtils.isNullOrEmpty(bundle)){
            id= bundle.getInt("driver_id");
        }
        mBinding.selectLove.setOnClickListener(v -> {
            if (!CollectionUtils.isNullOrEmpty(driverinfos)){{}
                Bundle bu =new Bundle();
                bu.putString("name",driverinfos.getRealname());
                bu.putString("avatar",driverinfos.getAvatar());
                bu.putDouble("experience",driverinfos.getExperience());
                bu.putSerializable("level",driverinfos.getLevel());
                startActivityForResult(RankingActivity.class,0, bu,true);
            }
        });
    }

    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
    }

    @Override
    protected void loadData(boolean isRefresh) {
        OkGo.<Driverinfo>get(AppConfig.Driverinfo)
                .tag(this)
                .params("id",id)
                .execute(new DialogCallback<Driverinfo>(this){
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Driverinfo> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                            driverinfos=response.body();
                            mBinding.name.setText(driverinfos.getRealname());
                            if (!CollectionUtils.isNullOrEmpty(driverinfos.getSex())){
                                if (driverinfos.getSex().equals("女")){
                                    mBinding.sex.setImageResource(R.mipmap.female);
                                }else{
                                    mBinding.sex.setImageResource(R.mipmap.male);
                                }
                            }

                            mBinding.sign.setText(driverinfos.getSignature());
                            if (driverinfos.getIs_driver()>0){
                                mBinding.driver.setText("已认证");
                                mBinding.driver.setBorderColor(getResources().getColor(R.color.green));
                                mBinding.driver.setTextColor(getResources().getColor(R.color.green));
                            }else{
                                mBinding.driver.setText("未认证");
                                mBinding.driver.setBorderColor(getResources().getColor(R.color.order));
                                mBinding.driver.setTextColor(getResources().getColor(R.color.order));
                            }
                            if (driverinfos.getIs_realname()>0){
                                mBinding.realname.setText("已认证");
                                mBinding.realname.setBorderColor(getResources().getColor(R.color.green));
                                mBinding.realname.setTextColor(getResources().getColor(R.color.green));
                            }else{
                                mBinding.realname.setText("未认证");
                                mBinding.realname.setBorderColor(getResources().getColor(R.color.order));
                                mBinding.realname.setTextColor(getResources().getColor(R.color.order));
                            }

                            Double total= driverinfos.getExperience();
                            int t = (new Double(total)).intValue();
                            mBinding.love.setText(t+"分");
                            if (!CollectionUtils.isNullOrEmpty(driverinfos.getAvatar())){
                                mBinding.logo.setImageURI(driverinfos.getAvatar());
                                Glide.with(LoveCartDetailsActivity.this).load(driverinfos.getAvatar()).asBitmap().into(new SimpleTarget<Bitmap>() {
                                    @Override
                                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                        Bitmap newBitmap = BlurKit.getInstance().blur( mBinding.logo, 10);
                                        mBinding.beijing.setImageBitmap(newBitmap);
                                    }
                                });
                            }
                            showData();
                        }else {
                            noData();
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<Driverinfo> response) {
                        super.onError(response);
                        showErrorView(response.code());
                    }
                });
    }

}
